package com.link.base.ext.emp.service;

import com.link.base.base.position.model.Position;
import com.link.base.base.position.service.PositionService;
import com.link.base.ext.emp.dao.mybatis.mapper.EmpPositionMapper;
import com.link.base.ext.emp.model.EmpPosition;
import com.link.base.ext.emp.model.ExtEmployee;
import com.link.base.user.model.CoreUser;
import com.link.core.basic.dao.mapper.BasicMapper;
import com.link.core.basic.service.BasicServiceException;
import com.link.core.basic.service.BasicServiceImpl;
import com.link.core.util.StringUtils;
import com.link.core.util.UserUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;

/**
 * 职位员工中间对象-
 *
 * @author 刘启鑫
 * @date 2020/09/03 16:51
 */
@Service
public class EmpPositionServiceImpl extends BasicServiceImpl<EmpPosition> implements EmpPositionService {

    @Resource
    private EmpPositionMapper empPositionMapper;

    @Resource
    private PositionService positionService;

    @Override
    public BasicMapper<EmpPosition> getBasicMapper() {
        return empPositionMapper;
    }

    @Override
    public void deleteEmpPostnByEmpId(EmpPosition empPosition) {
        empPositionMapper.deleteEmpPostnByEmpId(empPosition);
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<EmpPosition> getInsertEmpPostns(ExtEmployee employee, StringBuilder warn, String[] postnCodes) throws Exception {
        if (null == postnCodes || postnCodes.length == 0) {
            return null;
        }
        // 重复的数据列表
        Map<String, String> postnCodeMap = new HashMap<>(postnCodes.length);
        // 返回的列表
        List<EmpPosition> empPositionList = new ArrayList<>();
        for (String postnCode : postnCodes) {
            if (StringUtils.isNotBlank(postnCode)) {
                // 避免重复的协议编码
                if (StringUtils.isNotBlank(postnCodeMap.get(postnCode))) {
                    warn.append("[" + postnCode + "]职位编码重复;");
                    break;
                } else {
                    postnCodeMap.put(postnCode, postnCode);
                }
                EmpPosition empPosition = null;
                Position position = positionService.queryPositionByPostnCode(postnCode);
                if (!Objects.isNull(position)) {
                    // 当查询到职位才创建对象
                    empPosition = beforInsertSet(empPosition);
                    // 赋值职位ID和员工ID
                    empPosition.setEmpId(employee.getId()).setPostnId(position.getId());
                    empPositionList.add(empPosition);
                } else {
                    warn.append("[" + postnCode + "]职位无法查询到对应的职位;");
                }
            }
        }
        return empPositionList;
    }

    EmpPosition beforInsertSet(EmpPosition empPosition) {
        if (Objects.isNull(empPosition)) {
            empPosition = new EmpPosition();
        }
        CoreUser user = UserUtil.getUser();
        empPosition.setId(keyGenerateService.keyGenerate());
        empPosition.setOrgId(user.getOrgId());
        empPosition.setCorpid(user.getCorpid());
        empPosition.setLastUpdatedBy(user.getId());
        empPosition.setCreatedBy(user.getId());
        return empPosition;
    }


    @Override
    public void deleteAndInsertBatch(EmpPosition empPosition, List<EmpPosition> empPositionList) {
        if (Objects.isNull(empPosition)) {
            throw new BasicServiceException("员工职位中间表为空");
        }
        if (Objects.isNull(empPosition.getEmpId())) {
            throw new BasicServiceException("员工ID为空");
        }
        // 同时进行删除和新建，避免数据库锁
        empPositionMapper.deleteByEmpIdAndInsertBatch(empPosition.getEmpId(), empPositionList);
        /*
        deleteEmpPostnByEmpId(empPosition);
        if (!CollectionUtils.isEmpty(empPositionList)) {
            empPositionMapper.insertBatch(empPositionList);
        }*/
    }
}

